package com.changgou.oauth.controller;

import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.changgou.oauth.service.UserService;
import com.changgou.oauth.util.AuthToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.UnsupportedEncodingException;

/**
 * 用户登录
 */
/*@RestController
@RequestMapping("/user")*/
public class UserController {
    @Value("${auth.clientId}")
    private String clientId;

    @Value("${auth.clientSecret}")
    private String clientSecret;
    @Autowired
    private UserService userService;

    /**
     * 登录方法
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/login")
    public Result login(String username,String password) throws UnsupportedEncodingException {
        // 授权 方式
        String grant_type = "password";
        AuthToken token = userService.login(username, password, clientId, clientSecret, grant_type);
        if (token!=null)
        {
            return  new Result(true, StatusCode.OK, "登录成功",token);
        }
        return   new Result(false, StatusCode.LOGINERROR, "登录失败");
    }
}
